﻿using Entity.Models;
using HuaRunInterface.Comm;
using HuaRunInterface.Models;
using Microsoft.AspNetCore.Mvc;
using Utility;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HuaRunInterface.Controllers.GetControllers
{
    [Route("api/HuaRun/[controller]")]
    [ApiController]
    public class GetMeterInfoController : ControllerBase
    {
        // POST api/<GetMeterInfoController>
        [HttpPost]
        public object Post([FromBody] GetMeterParamModel value)
        {
            return GetDatas(value);
        }

        private object GetDatas(GetMeterParamModel dto)
        {
            ReturnData rd = new ReturnData();

            try
            {
                string conn = Global.Configuration["ConnectionStrings:DDb"];
                using (Entity.DBContext context = Entity.BaseContext.CreateContext(conn))
                {
                    int skip = dto.PageSize * (dto.PageIndex - 1);
                    rd.Total = (from p in context.meterInfos
                                where string.IsNullOrWhiteSpace(dto.meterCode) || p.meterCode.Contains(dto.meterCode)
                                where string.IsNullOrWhiteSpace(dto.deviceCode) || p.deviceCode == dto.deviceCode
                                where string.IsNullOrWhiteSpace(dto.protocolTypeCode) || p.protocolTypeCode == dto.protocolTypeCode
                                where string.IsNullOrWhiteSpace(dto.modelCode) || p.modelCode == dto.modelCode
                                where string.IsNullOrWhiteSpace(dto.commMode) || p.commMode == dto.commMode
                                where !dto.valveStatus.HasValue || p.valveStatus == dto.valveStatus
                                where string.IsNullOrWhiteSpace(dto.calcTypeCode) || p.calcTypeCode == dto.calcTypeCode
                                where p.IsDelete != 1
                                select p).Count();
                    List<MeterInfo> list = (from p in context.meterInfos
                                            where string.IsNullOrWhiteSpace(dto.meterCode) || p.meterCode.Contains(dto.meterCode)
                                            where string.IsNullOrWhiteSpace(dto.deviceCode) || p.deviceCode == dto.deviceCode
                                            where string.IsNullOrWhiteSpace(dto.protocolTypeCode) || p.protocolTypeCode == dto.protocolTypeCode
                                            where string.IsNullOrWhiteSpace(dto.modelCode) || p.modelCode == dto.modelCode
                                            where string.IsNullOrWhiteSpace(dto.commMode) || p.commMode == dto.commMode
                                            where !dto.valveStatus.HasValue || p.valveStatus == dto.valveStatus
                                            where string.IsNullOrWhiteSpace(dto.calcTypeCode) || p.calcTypeCode == dto.calcTypeCode
                                            where p.IsDelete != 1
                                            select p).SelfOrderBy(dto.OrderBy, dto.IsDesc).Skip(skip).Take(dto.PageSize).ToList();
                    if (list != null && list.Count > 0)
                    {
                        var v = (from p in list
                                 join pt in Global.ProtocolTypes on p.protocolTypeCode equals pt.Key into pt1
                                 from ppt in pt1.DefaultIfEmpty()
                                 join c in Global.CommunicationType on p.commMode equals c.Key into c1
                                 from cc in c1.DefaultIfEmpty()
                                 join m in Global.MeterType on p.meterTypeCode equals m.Key into m1
                                 from mm in m1.DefaultIfEmpty()
                                 join vxs in Global.ValveState on p.valveStatus.ToString() equals vxs.Key into vs1
                                 from vvs in vs1.DefaultIfEmpty()
                                 join g in Global.GasEnterDirection on p.inDirection equals g.Key into g1
                                 from gg in g1.DefaultIfEmpty()
                                 join ct in Global.ChargingType on p.calcTypeCode equals ct.Key into ct1
                                 from cct1 in ct1.DefaultIfEmpty()
                                 join mc in Global.MeterClassify on p.meterCategory equals mc.Key into mc1
                                 from mc11 in mc1.DefaultIfEmpty()
                                 select new
                                 {
                                     p,
                                     ProtocolTypeCodeName = ppt?.Value,
                                     CommModeName = cc?.Value,
                                     MeterTypeCodeName = mm?.Value,
                                     inDirectionName = gg?.Value,
                                     ValveStatusName = vvs?.Value,
                                     CalcTypeCodeName = cct1?.Value,
                                     meterCategoryName = mc11?.Value
                                 }); ;
                        rd.Data = v;
                    }
                    else
                    {
                        rd.Data = list;
                    }
                }
            }
            catch (Exception ex)
            {
                rd.State = "failed";
                rd.Message = ex.Message;
                rd.Data = ex.StackTrace;
            }
            return rd;
        }
    }

    public class GetMeterParamModel : GetBaseParamModels
    {
        public string? meterCode { get; set; }
        public string? deviceCode { get; set; }
        public string? protocolTypeCode { get; set; }
        public string? modelCode { get; set; }
        public string? commMode { get; set; }
        public int? valveStatus { get; set; }
        public string? calcTypeCode { get; set; }
    }
}
